﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CloudSoft.Extensions;

using ERPStore.Extensions;

using Microsoft.Practices.Unity;

namespace ERPStore.Payment.Direct.Services
{
	public class SandBoxPaymentService : ERPStore.Services.IPaymentService
	{
		private Models.SandboxSettings m_Settings;

		public SandBoxPaymentService()
		{
			var container = GlobalConfiguration.Configuration.DependencyResolver.GetContainer();
			m_Settings = container.Resolve<Models.SandboxSettings>("sandboxPaymentSettings");
		}

		public string CouponCode
		{
			get
			{
				return m_Settings.CouponCode;
			}
		}

		#region IPaymentService Members

		public bool IsAllowedFor(ERPStore.Models.OrderCart cart, ERPStore.Models.UserPrincipal principal)
		{
			if (cart.DiscountList.IsNullOrEmpty())
			{
				return false;
			}

			if (m_Settings.CouponCode.IsNullOrEmpty())
			{
				return false;
			}

			if (cart.DiscountList.Any(i => i.Code.Equals(m_Settings.CouponCode, StringComparison.InvariantCultureIgnoreCase)))
			{
				return true;
			}

			return false;
		}

		#endregion

		#region IPayment Members

		public string ConfirmationViewName
		{
			get
			{
				return "sandboxpaymentconfirmation.cshtml";
			}
		}

		public string FinalizedViewName
		{
			get
			{
				return m_Settings.FinalizedViewName ?? "sandboxpaymentfinalized.cshtml";
			}
		}

		public string ConfirmationRouteName
		{
			get
			{
				return "SandboxPaymentConfirmation";
			}
		}

		public string FinalizedRouteName
		{
			get
			{
				return "SandboxPaymentFinalized";
			}
		}

		public string Name
		{
			get { return "sandbox"; }
		}

		#endregion
	}
}
